package com.ujiuye.service.Impl;

import com.ujiuye.bean.Product;
import com.ujiuye.bean.QueryVO;
import com.ujiuye.service.ProductService;
import com.ujiuye.utils.SerializeUtil;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    JedisPool jedisPool;

    @Autowired
    HttpSolrClient solrClient;

    @Override
    public List<Product> findProduct(QueryVO queryVO) {
        Jedis jedis = jedisPool.getResource();
        //根据规则生成key
        String key = "offcn:dianshang:product:" + queryVO.getKeyWords();
        //判断redis中是否存在指定的key
        Boolean b = jedis.exists(key.getBytes());
        if (b) {
            System.out.println("redis中存在，直接查询redis");
            byte[] bytes = jedis.get(key.getBytes());
            List<Product> list_redis= (List<Product>) SerializeUtil.unserialize(bytes);
            return list_redis;
        } else {
            System.out.println("redis中不存在,查询solr");
            List<Product> productFromSolr = getProductFromSolr(queryVO);
            //查询后将数据存入redis中,下次查询就直接从redis中取值
            byte[] value = SerializeUtil.serialize(productFromSolr);
            jedis.set(key.getBytes(), value);
            return productFromSolr;
        }
    }


    public List<Product> getProductFromSolr(QueryVO queryVO) {
        List<Product> productList = new ArrayList<Product>();
        String keyWords = queryVO.getKeyWords();
        SolrQuery solrQuery = new SolrQuery("product_name:" + keyWords);
        solrQuery.setHighlight(true);
        solrQuery.addHighlightField("product_name");
        //设置高亮前缀
        solrQuery.setHighlightSimplePre("<em style='color:red'>");
        // 设置高亮后缀
        solrQuery.setHighlightSimplePost("</em>");
        try {
            QueryResponse query = solrClient.query(solrQuery);
            SolrDocumentList list = query.getResults();
            Map<String, Map<String, List<String>>> map = query.getHighlighting();
            for (int i = 0; i < list.getNumFound(); i++) {
                SolrDocument doc = list.get(i);
                String id = doc.getFieldValue("id") + "";
                String product_catalog = doc.getFieldValue("product_catalog") + "";
                String product_price = doc.getFieldValue("product_price") + "";
                String product_name1 = map.get(id).get("product_name").get(0);
                Product product = new Product();
                product.setId(Integer.parseInt(id));
                product.setProduct_name(product_name1);
                product.setProduct_catalog(product_catalog);
                product.setProduct_price(Double.valueOf(product_price));
                productList.add(product);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return productList;
    }


}
